package neu.edu.cn.nuemall.controller;

import neu.edu.cn.nuemall.dto.CartItemDTO;
import neu.edu.cn.nuemall.dto.OrderInfoDTO;
import neu.edu.cn.nuemall.entity.OrderInfo;
import neu.edu.cn.nuemall.entity.ShippingAddress;
import neu.edu.cn.nuemall.service.AddressService;
import neu.edu.cn.nuemall.service.BooksService;
import neu.edu.cn.nuemall.service.CartItemService;
import neu.edu.cn.nuemall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private BooksService booksService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartItemService cartService;

    @RequestMapping("/order-confirm")
    public String addToOrder(String skuId, Model model, String[] cartItems, String fromType)
    {
        if (fromType.equals("buy")) {
            if (StringUtils.hasText(skuId)) {
                List<CartItemDTO> cartItemList = new ArrayList<>();
                CartItemDTO cartItemDTO = new CartItemDTO();
                cartItemDTO.setCount(1);
                //查询商品规格信息
                cartItemDTO.setBooksSkuDTO(booksService.getBookSkuById(skuId));
                //查询商品信息
                cartItemDTO.setBooks(booksService.getBooksById(cartItemDTO.getBooksSkuDTO().getBooksId()));
                cartItemList.add(cartItemDTO);
                model.addAttribute("cartItemList", cartItemList);
                //查询当前用户的所有地址
                List<ShippingAddress> addressList = addressService.getAllAddressList();
                model.addAttribute("shippingAddressList", addressList);

                Integer booksCount = cartItemDTO.getCount();
                Double totalPrice = new Double(cartItemDTO.getBooksSkuDTO().getCurrentPrice());

                model.addAttribute("booksCount", booksCount);
                model.addAttribute("totalPrice", totalPrice);

                model.addAttribute("skuId", skuId);
            }
        }
        else if(fromType.equals("cart"))
        {
            if((cartItems!=null))
            {
                List<CartItemDTO> cartItemList = new ArrayList<>();
                Integer booksCount = 0;
                Double totalPrice =  new Double(0);
                for (String cartItemId : cartItems) {
                    CartItemDTO cartItemDTO = cartService.getCartItem(cartItemId);
                    cartItemList.add(cartItemDTO);
                    booksCount = booksCount + cartItemDTO.getCount();
                    totalPrice = totalPrice + cartItemDTO.getBooksSkuDTO().getCurrentPrice();
                }
                model.addAttribute("cartItemList", cartItemList);
                //查询当前用户的所有地址
                List<ShippingAddress> addressList = addressService.getAllAddressList();
                model.addAttribute("shippingAddressList", addressList);

                model.addAttribute("booksCount", booksCount);
                model.addAttribute("totalPrice", totalPrice);

                model.addAttribute("cartItems", cartItems);
            }
        }
        model.addAttribute("fromType", fromType);
        return "order/order-confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(String skuId,Integer booksCount,Double totalPrice,String shippingAddress,Model model,
                              String fromType, String[] cartItems){

        OrderInfo orderInfo = orderService.submitOrder(skuId,booksCount,totalPrice,shippingAddress,fromType,cartItems);
        model.addAttribute("orderId",orderInfo.getId());
        model.addAttribute("booksCount",booksCount);
        model.addAttribute("totalPrice",totalPrice);
        return "/order/order-pay";
    }


    @RequestMapping("/completePay")
    public String completeOrderPay(String orderId,Model model){
        int i=orderService.payOrder(orderId);
        //查询订单详情和商品信息
        OrderInfoDTO orderDto = orderService.getOrder(orderId);
        model.addAttribute("order", orderDto);
        return "/order/order-detail";
    }

    @GetMapping("/my-order")
    public String mallMyOrder(@RequestParam(value = "page",defaultValue = "1") Integer page,
                              Model model){

        model.addAttribute("orderlist",orderService.getOrderList(page));
        return "order/my-order";
    }
    @RequestMapping("/order-detail")
    public String orderDetail(String orderId,Model model){
        OrderInfoDTO orderDto = orderService.getOrder(orderId);
        model.addAttribute("order", orderDto);
        return "/order/order-detail";
    }
}
